<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 xsi exsl">

		
	<!-- 医嘱章节 -->
	<xsl:template match="*" mode="ProviderLabOrdersSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_ProviderOrders]" mode="ProviderLabOrdersEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 医嘱章节条目 (仅处理检验医嘱条目) -->
	<xsl:template match="*" mode="ProviderLabOrdersEntry">
		<xsl:apply-templates select="hl7:entry/hl7:organizer[(hl7:component/hl7:observation/hl7:code/@code='DE06.00.289.00') and (hl7:component/hl7:observation/hl7:value/@code='03')]" mode="HCCLabOrder">
			<xsl:with-param name="sectionType" select="'POL'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	
	<!-- 实验室检查章节 -->
	<xsl:template match="*" mode="LabStudiesSummarySection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_StudiesSummary]" mode="LabStudiesSummaryEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 实验室检查章节条目 -->
	<xsl:template match="*" mode="LabStudiesSummaryEntry">
		<xsl:choose>
			<!--  C0007 -->
			<xsl:when test="$hccDocType='C0007'">
				<xsl:apply-templates select="hl7:entry[hl7:organizer/hl7:component/hl7:observation/hl7:code/@code='DE04.30.019.00']" mode="HCCLabOrder">
					<xsl:with-param name="sectionType" select="'LSS'"/>
				</xsl:apply-templates>
			</xsl:when>
			
			<!--  C0001,C0011,C0012,C0013,C0019,C0032,C0033 -->
			<xsl:when test="($hccDocType='C0001') or ($hccDocType='C0011') or ($hccDocType='C0012') or ($hccDocType='C0013') or ($hccDocType='C0019') or ($hccDocType='C0032') or ($hccDocType='C0033')">
				<xsl:apply-templates select="hl7:entry" mode="HCCLabOrder">
					<xsl:with-param name="sectionType" select="'LSS'"/>
				</xsl:apply-templates>
			</xsl:when>
			
			<!--  C0002,C0003 -->
			<xsl:when test="($hccDocType='C0002') or ($hccDocType='C0003')">
				<xsl:apply-templates select="hl7:entry" mode="HCCLabOrder">
					<xsl:with-param name="sectionType" select="'LSS'"/>
				</xsl:apply-templates>
			</xsl:when>
			
			<!--  C0053 -->
			<xsl:when test="$hccDocType='C0053'">
				<xsl:apply-templates select="hl7:entry" mode="HCCLabOrder">
					<xsl:with-param name="sectionType" select="'LSS'"/>
				</xsl:apply-templates>
			</xsl:when>
			
			<!--  C0010 -->
			<xsl:when test="$hccDocType='C0010'">
				<xsl:apply-templates select="hl7:entry[(hl7:observation/hl7:code/@code='DE04.50.128.00') or (hl7:observation/hl7:code/@code='DE04.50.048.00') or (hl7:observation/hl7:code/@code='DE04.50.142.00') or (hl7:observation/hl7:code/@code='DE04.50.026.00') or (hl7:observation/hl7:code/@code='DE04.50.128.00')]" mode="HCCLabOrder">
					<xsl:with-param name="sectionType" select="'LSS'"/>
				</xsl:apply-templates>
			</xsl:when>
		</xsl:choose>	
	</xsl:template>
	
	
	<!--通用医嘱条目输出模板-->
	<xsl:template match="*" mode="HCCLabOrder">
		<xsl:param name = "sectionType" />
		<xsl:variable name="externalLabOrderNo" select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.24']/@extension"/>
			<LabOrder>
			<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
			<xsl:choose>
				<xsl:when test="$externalLabOrderNo !=''">
					<ExternalId><xsl:value-of select="concat($externalLabOrderNo,'-',position())"/></ExternalId>
					<PlacerId><xsl:value-of select="concat($externalLabOrderNo,'-',position())"/></PlacerId>
				</xsl:when>
				<xsl:otherwise>
					<ExternalId><xsl:value-of select="concat($hccDocNo,'-',$sectionType,'-',position())"/></ExternalId>
					<PlacerId><xsl:value-of select="concat($externalLabOrderNo,'-',position())"/></PlacerId>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:choose>
			    <!-- 医嘱章节 -->
				<xsl:when test="$sectionType='POL'">
					<OrderItem>
						<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:value"/></Code>
						<Description><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:value"/></Description>
					</OrderItem>
					<OrderQuantity><xsl:value-of select="1"/></OrderQuantity>
					<FromTime>
					<xsl:value-of select="isc:evaluate('xmltimestamp',hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:effectiveTime/hl7:high/@value)"/></FromTime>
					<ToTime>
					<xsl:value-of select="isc:evaluate('xmltimestamp',hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:effectiveTime/hl7:low/@value)"/></ToTime>
					<Status>
					<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.290.00']/hl7:value"/>
					</Status>
					<OrderedBy>
						<Name><GivenName>
						<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:author/hl7:assignedAuthor/hl7:assignedPerson/hl7:name"/>
						</GivenName></Name>
						<Code>
						<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:author/hl7:assignedAuthor/hl7:id/@extension"/>
						</Code>
					</OrderedBy>
					<VerifiedBy>
						<Name><GivenName>
						<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:participant/hl7:participantRole[hl7:code/@displayName='医嘱审核人']/hl7:playingEntity/hl7:name"/>
						</GivenName></Name>
						<Code>
						<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:participant/hl7:participantRole[hl7:code/@displayName='医嘱审核人']/hl7:id/@extension"/>
						</Code>
					</VerifiedBy>
					<Comments>
					<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.179.00']/hl7:value" />
					</Comments>
					<OrderCategory>
						<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.289.00']/hl7:value/@code"/></Code>
						<Description><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.289.00']/hl7:value/@displayName"/></Description>
					</OrderCategory>
				</xsl:when>
				<!-- 检验报告章节 -->
				<xsl:when test="$sectionType='LSS'">
					<xsl:choose>
					<!--  C0007 -->
					<xsl:when test="$hccDocType='C0007'">
					<OrderItem>
						<Code><xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:value"/></Code>
						<Description><xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:value"/></Description>
					</OrderItem>
					<OrderQuantity><xsl:value-of select="1"/></OrderQuantity>
					<FromTime>
					<xsl:value-of select="isc:evaluate('xmltimestamp',hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE04.50.134.00']/hl7:effectiveTime/hl7:low/@value)"/>
					</FromTime>
					<Specimen>
					<xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE04.50.134.00']/hl7:value"/>
					</Specimen>
					<SpecimenCollectedTime>
					<xsl:value-of select="isc:evaluate('xmltimestamp',hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE04.50.134.00']/hl7:effectiveTime/hl7:low/@value)"/>
					</SpecimenCollectedTime>
					<SpecimenReceivedTime>
					<xsl:value-of select="isc:evaluate('xmltimestamp',hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE04.50.134.00']/hl7:effectiveTime/hl7:high/@value)"/>
					</SpecimenReceivedTime>
					<Result>
					    <ResultType>AT</ResultType>
						<ResultItems>
							<xsl:apply-templates select="*" mode="HCCLabOrderResultItem">
							</xsl:apply-templates>
						</ResultItems>
					</Result>
					</xsl:when>
					
					<!--  C0001,C0011,C0012,C0013,C0019,C0032,C0033 -->
					<xsl:when test="($hccDocType='C0001') or ($hccDocType='C0011') or ($hccDocType='C0012') or ($hccDocType='C0013') or ($hccDocType='C0019') or ($hccDocType='C0032') or ($hccDocType='C0033')">
					<OrderItem>
						<Code><xsl:value-of select="'血型检查'"/></Code>
						<Description><xsl:value-of select="'血型检查'"/></Description>
					</OrderItem>
					<OrderQuantity><xsl:value-of select="1"/></OrderQuantity>
					<Specimen><xsl:value-of select="'血液'"/></Specimen>
					<Result>
					    <ResultType>AT</ResultType>
						<ResultItems>
							<LabResultItem>
								<SubId>1</SubId>
								<TestItemCode>
									<Code>Rh(D)血型</Code>
									<Description>Rh(D)血型</Description>
								</TestItemCode>
								<ResultCodedValue>
									<Code><xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.50.010.00']/hl7:value/@code"/></Code>
									<Description><xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.50.010.00']/hl7:value/@code"/></Description>
								</ResultCodedValue>
							</LabResultItem>
						</ResultItems>
						<ResultItems>
							<LabResultItem>
								<SubId>2</SubId>
								<TestItemCode>
									<Code>ABO血型</Code>
									<Description>ABO血型</Description>
								</TestItemCode>
								<ResultCodedValue>
									<Code><xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.50.001.00']/hl7:value/@code"/></Code>
									<Description><xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.50.001.00']/hl7:value/@code"/></Description>
								</ResultCodedValue>
							</LabResultItem>
						</ResultItems>
					</Result>					
					</xsl:when>
					
					<!--  C0002,C0003 -->
					<xsl:when test="($hccDocType='C0002') or ($hccDocType='C0003')">
					<OrderItem>
						<Code><xsl:value-of select="'辅助检查'"/></Code>
						<Description><xsl:value-of select="'辅助检查'"/></Description>
					</OrderItem>
					<OrderQuantity><xsl:value-of select="1"/></OrderQuantity>
					<Specimen><xsl:value-of select="'未知'"/></Specimen>
					<Result>
						<ResultItems>
							<LabResultItem>
								<SubId>1</SubId>
								<TestItemCode>
									<Code><xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.30.010.00']/hl7:value"/></Code>
									<Description><xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.30.010.00']/hl7:value"/></Description>
								</TestItemCode>
								<ResultValue><xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.30.009.00']/hl7:value"/></ResultValue>
							</LabResultItem>
						</ResultItems>
					</Result>
					</xsl:when>

					<!--  C0053 -->
					<xsl:when test="$hccDocType='C0053'">
					<OrderItem>
						<Code><xsl:value-of select="'辅助检查'"/></Code>
						<Description><xsl:value-of select="'辅助检查'"/></Description>
					</OrderItem>
					<OrderQuantity><xsl:value-of select="1"/></OrderQuantity>
					<Specimen><xsl:value-of select="'未知'"/></Specimen>
					<Result>
					    <ResultType>AT</ResultType>
						<ResultItems>
							<LabResultItem>
								<SubId>1</SubId>
								<TestItemCode>
									<Code><xsl:value-of select="'阳性辅助检查'"/></Code>
									<Description><xsl:value-of select="'阳性辅助检查'"/></Description>
								</TestItemCode>
								<ResultValue><xsl:value-of select="hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE04.50.128.00']/hl7:value"/></ResultValue>
							</LabResultItem>
						</ResultItems>
					</Result>
					</xsl:when>
					
					<!--  C0010 -->
					<xsl:when test="$hccDocType='C0010'">
					<OrderItem>
						<Code><xsl:value-of select="hl7:observation/hl7:code/@code"/></Code>
						<Description><xsl:value-of select="hl7:observation/hl7:code/@displayName"/></Description>
					</OrderItem>
					<OrderQuantity><xsl:value-of select="1"/></OrderQuantity>
					<Specimen><xsl:value-of select="'未知'"/></Specimen>
					<Result>
					    <ResultType>AT</ResultType>
						<ResultItems>
							<LabResultItem>
								<SubId>1</SubId>
								<TestItemCode>
									<Code><xsl:value-of select="hl7:observation/hl7:code/@code"/></Code>
									<Description><xsl:value-of select="hl7:observation/hl7:code/@displayName"/></Description>
								</TestItemCode>
								<ResultValue><xsl:value-of select="hl7:observation/hl7:value"/></ResultValue>
							</LabResultItem>
						</ResultItems>
					</Result>
					</xsl:when>
					
					</xsl:choose>
				</xsl:when>
			</xsl:choose>
			</LabOrder>		
	</xsl:template>
	
	<!--通用检验结果条目输出模板-->
	<xsl:template match="*" mode="HCCLabOrderResultItem">
	<!--		   <xsl:variable name="xpath">
				  <xsl:for-each select="ancestor-or-self::*">
					 <xsl:value-of select="name()"/>
					  test="not(position()=last())">
						<xsl:value-of select="'/'"/>
					 </xsl:if>
				  </xsl:for-each>
			   </xsl:variable>
			<p>$xPath: <xsl:value-of select="$xpath"/></p>
	-->		
		<LabResultItem>
			<SubId><xsl:value-of select="position()"/></SubId>
			<TestItemCode>
				<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:value"/></Code>
				<Description><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:value"/></Description>
			</TestItemCode>
			<ObservationTime><xsl:value-of select="isc:evaluate('xmltimestamp',hl7:component/hl7:observation[hl7:code/@code='DE04.30.019.00']/hl7:effectiveTime/@value)"/></ObservationTime>
			<ObservationMethods>
				<ObservationMethod>
					<Code><xsl:value-of select="../../hl7:entry[hl7:observation/hl7:code/@code='DE02.10.027.00']/hl7:observation/hl7:value"/></Code>
					<Description><xsl:value-of select="../../hl7:entry[hl7:observation/hl7:code/@code='DE02.10.027.00']/hl7:observation/hl7:value"/></Description>
				</ObservationMethod>
			</ObservationMethods>
			<ResultCodedValue>
				<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE04.30.017.00']/hl7:value/@code"/></Code>
				<Description><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE04.30.017.00']/hl7:value/@displayName"/></Description>
			</ResultCodedValue>
			<ResultValue><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE04.30.015.00']/hl7:value/@value"/></ResultValue>
			<ResultValueUnits>
			<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE04.30.015.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE04.30.016.00']/hl7:value"/>
			</ResultValueUnits>
		</LabResultItem>
	</xsl:template>
</xsl:stylesheet>
